#!/usr/bin/perl
use warnings;
use Data::Dumper;
use strict;
use Test::More tests => 20;

BEGIN{
	use_ok( 'Data::Form::Field::Option');
}

my %option1 = (
   		name       		=> 'before',
      	required   		=> 1,
        options    		=> [ qw/Mr Mrs Ms Dr/],
        default			=> 'Ms',
	  );

my $opt_struct = [ { label =>'Mr', value => 'Mr'}, { label =>'Mrs', value => 'Mrs'}, { label =>'Ms', value => 'Ms'} , { label =>'Dr', value => 'Dr'}];

can_ok('Data::Form::Field::Option',
	qw/ id default validators value errors label input_hint help readonly required valid many options/);

can_ok('Data::Form::Field::Option', qw/validate validate_custom get add/);


my $o = Data::Form::Field::Option->new( %option1 );

isa_ok($o,'Data::Form::Field::Option');

is( $o->name , 'before');

ok( $o->required, 'required flag is set');

is_deeply([$o->options], $opt_struct, 'Options struct looks good');

#diag( Dumper $o->options());

$o->fill_in('Sir');


is( $o->valid, 0,'Invalid Result');
like($o->errors, qr/NO_SUCH_OPTION/, 'Useful error message');

#ok($o->reset,'can reset');
#my $o1 = Data::Form::Field::Option->new( %option1 );
#is_deeply( $o, $o1, 'reset resets');

$o->fill_in('Dr');

is($o->value, "Dr",'retrieve value');

$o->fill_in('Mrs');

is($o->value, "Mrs",'value');

$o->reset;

is($o->fill_in('Mr'), 1, 'fill_in Mr is a valid value');
ok($o->valid, 'Mr is a valid value');

#===============================================================================
# One option
#

my $oo =  Data::Form::Field::Option->new( name => 'checkbox', options => ['Y']);

#diag(Dumper($oo->options));

ok($oo->fill_in('Y') == 1, 'Single Option is valid');
#diag( $oo->errors );



#===============================================================================
# Options_label
# Quite often you want to have an option that is an instruction in a select list
# e.g. Select a value from the list
# but you don't want this to be considered a valid option on submission of a required field 
# and you want to safely ignore it on submission of an optional field and apply the default
#
# HOw does deafult play into this?

my %options_label = ( 		
		name       		=> 'country',
		label				=> "What's your favourite country?",
      	options_label => 'Select from list',
        options    		=> [ qw/Canada Mexico Scotland USA/ ],
);

my $reqd_options_label = Data::Form::Field::Option->new(
		%options_label,
      	required   		=> 1,
);

$reqd_options_label->fill_in($options_label{options_label});
ok( $reqd_options_label->errors eq 'NO_SUCH_OPTION' , 'required field chokes on options label');
$reqd_options_label->fill_in('Scotland');
ok( $reqd_options_label->value eq 'Scotland', 'options_label field has correct value');
ok( $reqd_options_label->errors, 'valid fields have no errors');

my $options_label = Data::Form::Field::Option->new(
		%options_label,
		default	=> 'Canada'
);

ok( $options_label->fill_in($options_label{options_label}), 'optional field ok with options label'); 
ok( $options_label->value eq $options_label->default, 'optional field defaults with options label'); 
ok( $options_label->valid, 'optional field is valid with options label');
